import html2canvas from 'html2canvas'

// 截图方法
export function cutPicture (element) {
  const width = element.clientWidth
  const height = element.clientHeight
  const canvas = document.createElement('canvas')
  const scale = 2
  // 宽高扩大 2 倍 处理图片模糊
  canvas.width = width * scale
  canvas.height = height * scale
  canvas.getContext('2d').scale(scale, scale)
  const options = {
    width: width,
    height: height,
    canvas: canvas,
    useCORS: true,
    logging: true,
    scale: 1,
    dpi: 300
  }
  html2canvas(element, options).then((canvas) => {
    const context = canvas.getContext('2d')
    context.scale(scale, scale)
    context.mozImageSmoothingEnabled = false
    context.webkitImageSmoothingEnabled = false
    context.msImageSmoothingEnabled = false
    context.imageSmoothingEnabled = false
    // 生成图片地址
    const imgUrl = canvas.toDataURL()
    // console.log('生成的截图下载链接：', this.finalImg)

    // 下载图片
    const elink = document.createElement('a')
    elink.download = '频谱快照.png'
    elink.style.display = 'none'
    elink.href = imgUrl
    document.body.appendChild(elink)
    elink.click()
    document.body.removeChild(elink)
  })
}
